﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCTLib.Common;
using System.Data;

namespace DCTAccess.Business.Location
{
    public class LocationController
    {
        public static bool Insert(Location objLocation)
        {
            return ExecuteStoredProcedure(objLocation, StoreProceduAction.AddNew.ToString());
        }

        public static bool Update(Location objLocation)
        {
            return ExecuteStoredProcedure(objLocation, StoreProceduAction.Update.ToString());
        }

        public static bool Delete(Location objLocation)
        {
            return ExecuteStoredProcedure(objLocation, StoreProceduAction.Delete.ToString());
        }

        public static bool ExecuteStoredProcedure(Location objLocation, string action)
        {
            SQLHelper sqlHelper = new SQLHelper();
            string sPro = "prLocation";
            sqlHelper.AddParameter("@Action", action);
            sqlHelper.AddParameter("@LocationID", objLocation.LocationID);
            sqlHelper.AddParameter("@LocationName", objLocation.LocationName);
            //sqlHelper.AddParameter("@LocationAlias", objLocation.LocationAlias);
            sqlHelper.AddParameter("@ParentID", objLocation.ParentID);
            sqlHelper.AddParameter("@IsEnable", objLocation.IsEnable);
            return sqlHelper.ExecuteSQL(sPro);
        }

        public static LocationCollection GetLocation(LocationFilter searchCriteria)
        {
            DataTable ltDatatable = GetLocationByFilter(searchCriteria);

            if (ValidationHelper.HasRow(ltDatatable))
            {
                return new LocationCollection(ltDatatable);
            }
            else
            {
                return null;
            }
        }

        public static Location GetLocationByID(int locationID)
        {
            LocationFilter searchCriteria = new LocationFilter();
            searchCriteria.LocationID = locationID;

            DataTable dtLocation = searchCriteria.GetLocation();
            if (ValidationHelper.HasRow(dtLocation))
            {
                return new Location(dtLocation.Rows[0]);
            }
            else
            {
                return null;
            }
        }

        public static DataTable GetListLocation()
        {
            SQLHelper sqlHelper = new SQLHelper();
            string sProc = "Location_GetAll";
            return sqlHelper.GetDataTable(sProc);
        }

        public static DataTable GetLocationByFilter(LocationFilter searchCriteria)
        {
            return searchCriteria.GetLocation();
        }

        public static DataTable GetLocationDetails()
        {
            LocationFilter filter = new LocationFilter();
            return filter.GetLocation();
        }       


        public static LocationCollection GetAllLocation() 
        {
            SQLHelper sqlHelper = new SQLHelper();
            string sProc = "Location_GetAll";
            DataTable dtLocation = sqlHelper.GetDataTable(sProc);
            if (ValidationHelper.HasRow(dtLocation))
            {
                return new LocationCollection(dtLocation);
            }
            else
            {
                return null;
            }
        }       
    }
}
